﻿//--------------------------------------------------
// TXna Game Engine
// This file belongs to the TXnaGE
// https://sourceforge.net/projects/txnagameengine/
//--------------------------------------------------


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace sqengine.PhysicObjects
{

    /// <summary>
    /// Holds all information about lights in a scene. This class is for internal TXna infrastructure support.
    /// </summary>
    public sealed class SceneLights
    {
        private byte directionalLightCount;
        private byte pointLightCount;
        private byte spotLightCount;
        private byte totalLightCounter;

        private Vector4[] positions = new Vector4[10];   //  x,y,z, light type (0 dir, 1, point, 2 spot)
        private Vector3[] directions = new Vector3[10];  //  x,y,z
        private Vector3[] colors = new Vector3[10];      //  RGB
        private Vector4[] data = new Vector4[10];        //  range, falloff theta, phi

        public void Reset()
        {
            totalLightCounter = 0;
            directionalLightCount = 0;
            pointLightCount = 0;
            spotLightCount = 0;
            positions = new Vector4[10];   //  x,y,z, light type (0 dir, 1, point, 2 spot)
            directions = new Vector3[10];  //  x,y,z
            colors = new Vector3[10];      //  RGB
            data = new Vector4[10];        //  range, falloff theta, phi
        }

        public void AddLight(Light light)
        {
            //  add the light to corresponding array
            if (light.LightType == LightType.Directional)
            {
                //  TODO: check if there are other ways to represent directional lights. The shaders only need the direction but the current shadow mapper needs the position (which is in infinity for dir lights).
                directionalLightCount++;
                Vector3 position = -light.Direction;    //  reverse direction 
                //position.Normalize();
                //position *= 10000;
                light.SetPosition(ref position);
            }
            else if (light.LightType == LightType.Point)
                pointLightCount++;
            else
                spotLightCount++;

            positions[totalLightCounter] = new Vector4(light.Position, (float)light.LightType);
            colors[totalLightCounter] = light.Color;
            directions[totalLightCounter] = light.Direction;
            data[totalLightCounter] = light.SpotData;
            totalLightCounter++;
        }

        public void GetLightArrays(out Vector4[] positions, out Vector3[] directions, out Vector3[] colors, out Vector4[] data)
        {
            positions = this.positions;
            colors = this.colors;
            directions = this.directions;
            data = this.data;
        }

        /// <summary>
        /// Holds shadow maps, one per each light. 
        /// </summary>
        public Texture2D[] ShadowMaps;
        public byte DirectionalLightCount { get { return directionalLightCount; } }
        public byte PointLightCount { get { return pointLightCount; } }
        public byte SpotLightCount { get { return spotLightCount; } }
        public byte TotalLightCount { get { return totalLightCounter; } }
    }
}
